package com.stegniy.anagram;

import java.util.*;

public class AnagramFinder {

    public Map<String,List<String>> anagramMap = new HashMap<String, List<String>>();

    public List<List<String>> findAnagram(List<String> wordsList) {
        List<List<String>> result = new ArrayList<List<String>>();
        List<String> l = new ArrayList<String>();

        for (String w : wordsList) {
            String key = alphabetize(w);
                l = anagramMap.get(key);
                if (l == null)
                    anagramMap.put(key, l=new ArrayList<String>());
                l.add(w);
        }
        for (Map.Entry<String, List<String>> e : anagramMap.entrySet()) {
            if ((e.getKey().length() > 2) & (e.getValue().size() > 1)) {
                result.add(e.getValue());
            }
        }
        return result;
    }

    private static String alphabetize(String s) {
        char[] a = s.toCharArray();
        Arrays.sort(a);
        return new String(a);
    }
}
